package com.lindroy.androidplayer.ui.mine

import android.os.Bundle
import android.view.View
import com.lindroy.androidplayer.R
import com.lindroy.androidplayer.base.App
import com.lindroy.androidplayer.base.BaseListFragment
import com.lindroy.androidplayer.databinding.FragmentMyShareBinding
import com.lindroy.androidplayer.ui.adapter.ArticleAdapter
import com.lindroy.androidplayer.ui.webview.WebViewFragment
import com.lindroy.androidplayer.utils.addSimpleDivider
import com.lindroy.androidplayer.utils.collectListWhenResumed
import com.lindroy.androidplayer.utils.collectWhenResumed
import com.lindroy.androidplayer.viewmodels.MyShareViewModel

/**
 * @author Lin
 * @date 2022/2/25
 * @function 我的分享
 */
class MyShareFragment : BaseListFragment<FragmentMyShareBinding, MyShareViewModel>() {

    private val adapter by lazy { ArticleAdapter(viewLifecycleOwner) }

    override fun FragmentMyShareBinding.initView(savedInstanceState: Bundle?) {
        initToolbar(R.string.mine_share)
        vm = mViewMode
        rvShare.addSimpleDivider()
        rvShare.adapter = adapter.withFooter()
    }

    override fun FragmentMyShareBinding.initListener() {
        mViewMode.setLayoutStateFlow(adapter.loadStateFlow)
        mViewMode.shareArticles.collectListWhenResumed(viewLifecycleOwner){
            adapter.submitData(it)
        }
        mViewMode.itemClickLiveData.observe(viewLifecycleOwner){
            WebViewFragment.start(this@MyShareFragment, it.id, it.link, it.title, it.collect)
        }
        App.eventViewModel.loginInfoFlow.collectWhenResumed(viewLifecycleOwner){
            adapter.refresh()
        }
    }

    override fun onRefresh() {
        adapter.refresh()
    }

    override fun onRetry(view: View) {
        adapter.retry()
    }

}